package com.im.activity;


import org.jivesoftware.smack.ReconnectionManager;

import roboguice.inject.InjectView;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.core.sdk.core.BaseActivity;
import com.core.sdk.core.BaseEvent;
import com.core.sdk.core.UITask;
import com.im.R;
import com.im.event.UserRequestEvent;
import com.im.event.UserRequestEvent.OPERATE;
import com.im.event.UserResponseEvent;
import com.im.service.SmackService;

public class RegisterActivity extends BaseActivity implements OnClickListener {

	
	@InjectView(R.id.act_reg_et_phone)
	EditText etPhone;
	
	@InjectView(R.id.act_reg_et_pwd)
	EditText etPwd;
	
	@InjectView(R.id.act_reg_et_name)
	EditText etName;
	
	@InjectView(R.id.act_reg_et_email)
	EditText etEmail;
	
	@InjectView(R.id.act_reg_btn_ok)
	Button btnOK;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		btnOK.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		if(v == btnOK){
			String phone = etPhone.getText().toString().trim();
			String password = etPwd.getText().toString().trim();
			String name = etName.getText().toString().trim();
			String email = etEmail.getText().toString().trim();
			UserRequestEvent evt = new UserRequestEvent(findLocation(SmackService.class),OPERATE.request_reg,phone, password);
			evt.setName(name);
			evt.setEmail(email);
			sendEvent(evt);
		}
	}
	@Override
	public void executeEvent(BaseEvent evt) {
		super.executeEvent(evt);
		if(evt instanceof UserResponseEvent){
			UserResponseEvent event = (UserResponseEvent)evt;
			if(event.getResponseCode() ==UserResponseEvent.RESPONSE_REG_SUC){
				postRunOnUi(new UITask() {
					@Override
					public void run() {
						setResult(Activity.RESULT_OK);
						finish();
					}
				});
			}
		}
	}

	@Override
	protected int getContentView() {
		return R.layout.act_reg;
	}

}
